/**
 * 
 */
package cyclopean.world;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Set;

import javagame.core.io.Screen;
import javagame.core.io.keyboard.KeyActionMap;
import javagame.core.main.Time;
import javagame.impl.io.keyboard.ActionMapping;
import javagame.impl.io.keyboard.KeyBinding;

// TODO Javadoc
/**
 * 
 *
 * @author Jaco van der Westhuizen
 */
public class WorldScreen extends Screen
{
	private static final String KEYS_FILE = "./cyclopean.keys"; 

	public static enum Action implements ActionMapping
	{
	    FORWARD(KeyEvent.VK_W, "FORWARD"),
	    BACKWARD(KeyEvent.VK_S, "BACKWARD"),
	    STRAFE_LEFT(KeyEvent.VK_A, "STRAFE_LEFT"),
	    STRAFE_RIGHT(KeyEvent.VK_D, "STRAFE_RIGHT"),
	    UPWARD(KeyEvent.VK_SPACE, "UPWARD"),
	    DOWNWARD(KeyEvent.VK_SHIFT, "DOWNWARD");

	    final int key;
	    final String name;

	    private Action (int key, String name)
	    {
	    	this.key = key;
	    	this.name = name;
	    }

		/* (non-Javadoc)
         * @see javagame.impl.io.keyboard.ActionMapping#getDefaultKey()
         */
        @Override
        public int getDefaultKey()
        {
	        return key;
        }

		/* (non-Javadoc)
         * @see javagame.impl.io.keyboard.ActionMapping#getText()
         */
        @Override
        public String getText()
        {
	        return name;
        }
	}
	
	private static final KeyBinding KEYS = new KeyBinding(KEYS_FILE, Action.values());

	final SkyRenderer skyRender;
	final WorldRenderer worldRender;
	final Player player;

	/**
     * @param world
     */
    WorldScreen()
    {
    	this.player = new Player();
    	this.worldRender = new WorldRenderer(player);
    	this.skyRender = new SkyRenderer(player);

    	// TODO 3D anaglyph rendering
    	addScreenRenderer(skyRender);
    	addScreenRenderer(worldRender);
    	// TODO Border 'vignette' to limit vision during darkness or pain
    	// TODO Cross-hair?
    	// TODO GUIs and MouseCursor
    }
    
	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#getKeyActionMap()
	 */
	@Override
	public KeyActionMap<ActionMapping> getKeyActionMap()
	{
		return KEYS;
	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onCharTyped(char)
	 */
	@Override
	public void onCharTyped(char character)
	{
	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onKeyActionOn(java.lang.Object)
	 */
	@Override
	public void onKeyActionOn(Set<?> actions)
	{
		synchronized(player.action)
		{
			for (Object action : actions)
			{
				player.action[((Action)action).ordinal()] = true;
			}
		}
	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onKeyActionOff(java.lang.Object)
	 */
	@Override
	public void onKeyActionOff(Set<?> actions)
	{
		synchronized(player.action)
		{
			for (Object action : actions)
			{
				player.action[((Action)action).ordinal()] = false;
			}
		}
	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onMouseDown(int)
	 */
	@Override
	public void onMouseDown(int button)
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onMouseMove()
	 */
	@Override
	public void onMouseMove()
	{
		// TODO Auto-generated method stub
		System.out.println(mouseX + ", " + mouseY);
		try
        {
	        Robot r = new Robot();
	        r.mouseMove(0, 0);
        }
        catch (AWTException e)
        {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onMousePick()
	 */
	@Override
	public void onMousePick()
	{
	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onMouseUp(int)
	 */
	@Override
	public void onMouseUp(int button)
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javagame.core.io.Screen#onMouseWheel(int)
	 */
	@Override
	public void onMouseWheel(int diff)
	{
	}

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#update(javagame.core.main.Time)
     */
    @Override
    public void update(Time time)
    {
	    player.update();

	    super.update(time);
    }
}
